package com.euc.springbootframework.dao.base;

import com.euc.springbootframework.dao.base.impl.BaseBaseDaoImpl;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;

import java.io.Serializable;

public class BaseDaoFactory extends MongoRepositoryFactory {

    private final MongoOperations mongoOperations;

    public BaseDaoFactory(final MongoOperations mongoOperations) {
        super(mongoOperations);
        this.mongoOperations = mongoOperations;
    }

    @Override protected Class<?> getRepositoryBaseClass(final RepositoryMetadata metadata) {
        return BaseBaseDaoImpl.class;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    protected Object getTargetRepository(RepositoryInformation information) {

        MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());
        return new BaseBaseDaoImpl(entityInformation, this.mongoOperations);
    }
}
